package com.twu.biblioteca;

/**
 * Created by martinagehwolf on 23/07/2014.
 */

public class Library {

    private LibraryItemList libraryItemList;
    private UserList userList;
    private User currentUser;
    private UI input;

    public Library(UI input) {
        libraryItemList = new LibraryItemList();
        userList = new UserList();
        addBook(1, "The Eye of the World", "Robert Jordan", "2012", true);
        addBook(2, "A Song of Ice and Fire", "George RR Martin", "2012", true);
        addBook(3, "Lord of the Rings", "J. R. R. Tolken", "2012", true);
        this.input = input;
    }

    public void setUp(){
        welcomeMessage();
        listMenuOptions();
        chooseMenuOption(getMenuChoice());
    }

    public int getMenuChoice() {
        return Integer.parseInt(input.read());
    }

    public void chooseMenuOption(int choice) {
        switch (choice){
            case 0: listMenuOptions();
                break;
            case 1: libraryItemList.show(true, "Book");
                break;
            case 2: libraryItemList.show(true, "Movie");
                break;
            case 3: checkOutItem("Book");
                break;
            case 4: returnBook();
                break;
            case 5: checkOutItem("Movie");
                break;
            case 6: //returnMovie
                break;
            case 7: logon();
                break;
            case 8: viewUserList();
                break;
            case 9: viewPersonalDetails();
                break;
            default: System.out.println("Invalid Menu Option:");
                chooseMenuOption(0);
        }
        System.out.println();
    }

    public void viewPersonalDetails() {
        System.out.println(currentUser.getName()+", "+ currentUser.getEmail() + ", " + currentUser.getPhone());
    }

    public void viewUserList() {
        userList.viewUserList();
        System.out.println("Please select User to be viewed");
        String code = input.read();
        LibraryItemList selectedLibraryItemList = userList.viewUserList(Integer.parseInt(code));
        selectedLibraryItemList.show(true, "Book");
        selectedLibraryItemList.show(true, "Movie");
    }

    public void listMenuOptions() {
        System.out.println("The following options are available");
            for (MenuChoice choice : MenuChoice.values()) {
                System.out.println(choice.getMenuCode() +") "+choice.getMenuMessage());
            }
        System.out.println("Please type number and press enter for the desired Menu Option");
    }

    public void welcomeMessage() {
        System.out.println("WELCOME TO THE BANGALORE PUBLIC LIBRARY!");
    }

    public void checkOutItem(String Item) {
        if (Item.equals("Book")) showBookList(true);
        else    showMovieList(true);

        System.out.println("Please select Item to be checked out");
        String code = input.read();
        currentUser.getLibraryItemList().checkOutItem(Integer.parseInt(code));
    }

    public void addBook(int code, String name, String author, String year, boolean availability) {
        libraryItemList.addItem(code, name, author, year, availability);
    }

    public void showBookList(boolean availability) {
        libraryItemList.show(availability, "Book");
    }

    public void returnBook() {
        showBookList(false);
        System.out.println("Please select book to be returned");
        String code = input.read();
        currentUser.getLibraryItemList().returnBook(Integer.parseInt(code));
    }

    public void addMovie(int code, String name, String year, String creator, boolean availability, int rating) {
        libraryItemList.addItem(code, name, creator, year, availability, rating);
    }

    public void showMovieList(boolean availability) {
        libraryItemList.show(availability, "Movie");
    }

    public void logon() {
        System.out.println("Please enter: LibraryNumber(xxx-xxxx) Password");
        String[] usernamePassword = input.read().split(" ");
        currentUser = userList.validateUser(usernamePassword);
    }
}
